package semant.first_pass;

import semant.Env;
import symbol.ClassInfo;
import symbol.MethodInfo;
import symbol.Symbol;
import syntaxtree.IntegerType;
import syntaxtree.MainClass;
import syntaxtree.VisitorAdapter;

public class MainClasslHandler extends VisitorAdapter {

	private Env env;

	private MainClasslHandler(Env e) {
		super();
		env = e;
	}

	public static void firstPass(Env result, MainClass mainClass) {

		MainClasslHandler h = new MainClasslHandler(result);
		mainClass.accept(h);
	}

	@Override
	public void visit(MainClass node) {
		Symbol name = Symbol.symbol(node.className.s);
		ClassInfo info = new ClassInfo(name);
		
		MethodInfo m = new MethodInfo(
				new IntegerType(node.line, node.row), 
				Symbol.symbol("Main"),
				info.name);
		
		info.methods.put(m.name, m);
		
		if (!env.classes.put(name, info)) {
			env.err.Error(node.className, new Object[] {
				"Redeclaracaoo da classe: \'" + name + "\'.",
			});
		}
	}

}
